var addr=window.sessionStorage.getItem('url');
var url=addr+"listWantedBookInfo.htm";
var myXmlHttpRequest;
var username=window.sessionStorage.getItem('username');
var school=window.sessionStorage.getItem('school');

if(username!=null){
	document.getElementById('link_user_name').innerHTML=username;
}
if(school!=null){
	document.getElementById("link_school").innerHTML=school;
}

myXmlHttpRequest = new XMLHttpRequest();
myXmlHttpRequest.open("get",url,true);
myXmlHttpRequest.onreadystatechange=progress;
myXmlHttpRequest.send(null);

function progress(){
	if(myXmlHttpRequest.readyState==4){
		var json=myXmlHttpRequest.responseText;
		var json_obj=eval("("+json+")");
		var meta=json_obj.meta;
		if(meta.state=="success"){
			var per=json_obj.data;
			var ulBody=document.getElementById("elems");
			ulBody.innerHTML='';
			for(var i=0;i<per.length;i++){
				var li=document.createElement('li');
				var img=document.createElement('img');
				img.id="getpic"+i;
				img.className="picture";
				li.appendChild(img);
				var div1=document.createElement('div');
				div1.className="book_name";
				div1.innerHTML='<a href="javascript:void(0)" onclick="linkBookInfo('+per[i].id+')"'+'>'+per[i].bookname+'</a>';
				li.appendChild(div1);
				var div2=document.createElement('div');
				div2.className="price";
				div2.innerHTML=per[i].price;
				li.appendChild(div2);
				var div3=document.createElement('div');
				div3.className="school";
				div3.innerHTML=per[i].school;
				li.appendChild(div3);
				ulBody.appendChild(li);
				loadpic(per[i].id,img.id);
			}

		}
		else{
			window.alert(meta.message);
		}
	}
}

function loadpic(picId,imgId){
	var tempUrl=addr+"loadWantedPicById.htm?id="+picId+"";
	myXmlHttpRequest = new XMLHttpRequest();
	myXmlHttpRequest.open("get",tempUrl,true);
	myXmlHttpRequest.responseType = "blob";
	myXmlHttpRequest.onload = function() {
        if (this.status == 200) {
        	var blob=new Blob();
			blob = this.response;
			var img=document.getElementById(imgId);
			img.onload=function(e){
				window.URL.revokeObjectURL(img.src);
			};
			img.src=window.URL.createObjectURL(blob);
		};
	}
	myXmlHttpRequest.send(null);
}

function linkBookInfo(id){
	var tmpurl="WantedInfo.html?id="+id;
	window.location.href=tmpurl;
}

